<?php

namespace app\admin\model\xiluedu;

use think\Model;


class Coupon extends Model
{

    

    

    // 表名
    protected $name = 'xiluedu_coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'receive_start_time_text',
        'receive_end_time_text',
        'use_start_time_text',
        'use_end_time_text',
        'type_text',
        'freight_type_text',
        'range_status_text',
        'status_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getFreightTypeList()
    {
        return ['1' => __('Freight_type 1'), '2' => __('Freight_type 2'), '3' => __('Freight_type 3')];
    }

    public function getRangeStatusList()
    {
        return ['1' => __('Range_status 1'), '2' => __('Range_status 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getReceiveStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['receive_start_time']) ? $data['receive_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReceiveEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['receive_end_time']) ? $data['receive_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUseStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_start_time']) ? $data['use_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUseEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_end_time']) ? $data['use_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFreightTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['freight_type']) ? $data['freight_type'] : '');
        $list = $this->getFreightTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRangeStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['range_status']) ? $data['range_status'] : '');
        $list = $this->getRangeStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setReceiveStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReceiveEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUseStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUseEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    /**
     * 优惠券使用商品
     */
    public function addCouponCourse($course_ids){
        if(!$course_ids) return false;
        $coupon_course = [];
        foreach ($course_ids as $goods_id){
            $coupon_course[] = ['coupon_id'=>$this->id,'course_id'=>$goods_id,'createtime'=>time()];
        }
        return (new CouponCourse())->saveAll($coupon_course);
    }


}
